package org.wsiz.vro.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.data.domain.Persistable;

import com.google.common.base.Objects;

/**
 * @author "Tomasz Marciniak"
 */
@Entity
@Table(name = "address")
public class Address implements Persistable<Integer> {
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(Address.class);

	private Integer id;
	private String streetName;
	private String streetNo;
	private String buildingNo;
	private String city;
	private Integer version;

	public Address() {
	}

	public Address(Integer primaryKey) {
		setId(primaryKey);
	}

	@Override
	@Transient
	@XmlTransient
	public boolean isNew() {
		return getId() == null;
	}

	@Column(nullable = false, unique = true, precision = 10)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
	@Id
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Size(max = 255)
	@Column(name = "street_name")
	public String getStreetName() {
		return streetName;
	}

	public void setStreetName(String streetName) {
		this.streetName = streetName;
	}

	@Column(name = "street_no")
	public String getStreetNo() {
		return streetNo;
	}

	public void setStreetNo(String streetNo) {
		this.streetNo = streetNo;
	}
	
	@Column(name = "building_no")
	public String getBuildingNo() {
		return buildingNo;
	}

	public void setBuildingNo(String buildingNo) {
		this.buildingNo = buildingNo;
	}

	@NotEmpty
	@Size(max = 255)
	@Column(nullable = false)
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@Column(precision = 10)
	@Version
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	/**
	 * Set the default values.
	 */
	public void initDefaultValues() {
	}

	@Override
	public boolean equals(Object other) {
		return this == other
				|| (other instanceof Address && hashCode() == other.hashCode());
	}

	private PersistableHashBuilder persistableHashBuilder = new PersistableHashBuilder();

	@Override
	public int hashCode() {
		return persistableHashBuilder.hash(this);
	}

	@Override
	public String toString() {
		return Objects.toStringHelper(this) //
				.add("id", getId()) //
				.add("streetName", getStreetName()) //
				.add("streetNo", getStreetNo()) //
				.add("buildingNo", getBuildingNo()) //
				.add("city", getCity()) //
				.add("version", getVersion()) //
				.toString();
	}
}